home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Offline Browsing / HTTrack.exe / data1.cab / Sources / src / htsparse.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-04-28  |  90.3 KB  |  1,933 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: Main source                                            */
  34. /* DIRECT INCLUDE TO httrack.c                                  */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38.  
  39. #if HTS_ANALYSTE
  40. if (hts_htmlcheck(r.adr,(int)r.size,urladr,urlfil)) {
  41. #endif          
  42.   FILE* fp=NULL;      // fichier Θcrit localement                                               // et si level>0
  43.   char* adr=r.adr;    // pointeur (on parcourt)
  44.   char* lastsaved;    // adresse du dernier octet sauvΘ + 1
  45.   if ( (opt.debug>1) && (opt.log!=NULL) ) {
  46.     fspc(opt.log,"debug"); fprintf(opt.log,"scan file.."LF); test_flush;
  47.   }
  48.  
  49.  
  50.   // Indexing!
  51. #if HTS_MAKE_KEYWORD_INDEX
  52.   if (opt.kindex) {
  53.     if (index_keyword(r.adr,r.size,r.contenttype,savename,opt.path_html)) {
  54.       if ( (opt.debug>1) && (opt.log!=NULL) ) {
  55.         fspc(opt.log,"debug"); fprintf(opt.log,"indexing file..done"LF); test_flush;
  56.       }
  57.     } else {
  58.       if ( (opt.debug>1) && (opt.log!=NULL) ) {
  59.         fspc(opt.log,"debug"); fprintf(opt.log,"indexing file..error!"LF); test_flush;
  60.       }
  61.     }
  62.   }
  63. #endif
  64.  
  65.   // Now, parsing
  66.   if ((opt.getmode & 1) && (ptr>0)) {  // rΘcupΘrer les html sur disque       
  67.     // crΘer le fichier html local
  68.     HT_ADD_FOP;   // Θcrire peu α peu le fichier
  69.   }
  70.   
  71.   if (!error) {
  72.     int detect_title=0;  // dΘtection  du title
  73.     //
  74.     char* in_media=NULL; // in other media type (real media and so..)
  75.     int intag=0;         // on est dans un tag
  76.     int incomment=0;     // dans un <!--
  77.     int inscript=0;      // dans un scipt pour applets javascript)
  78.     int inscript_tag=0;  // on est dans un <body onLoad="... terminΘ par >
  79.     char inscript_tag_lastc='\0';  
  80.                            // terminaison (" ou ') du "<body onLoad=.."
  81.     int inscriptgen=0;     // on est dans un code gΘnΘrant, ex aprΦs obj.write("..
  82.     char scriptgen_q='\0'; // caractΦre faisant office de guillemet (' ou ")
  83.     int nofollow=0;        // ne pas scanner
  84.     //
  85.     int parseall_lastc='\0';    // dernier caractΦre parsΘ pour parseall
  86.     int parseall_incomment=0;   // dans un /* */ (exemple: a = /* URL */ "img.gif";)
  87.     //
  88.     char* intag_start=adr;
  89.     int intag_start_valid=0;
  90.     HT_ADD_START;    // dΘbuter
  91.  
  92.  
  93.     /* statistics */
  94.     if ((opt.getmode & 1) && (ptr>0)) { 
  95.       HTS_STAT.stat_files++;
  96.       HTS_STAT.stat_bytes+=r.size;
  97.     }
  98.  
  99.  
  100.     /* Check is the file is a .js file */
  101.     if (strfield2(r.contenttype,"application/x-javascript")!=0) {      /* JavaScript js file */
  102.       inscript=1;
  103.       intag=1;     // because aprΦs <script> on y est .. - pas utile
  104.       intag_start_valid=0;    // OUI car nous sommes dans du code, plus dans du "vrai" tag
  105.       if ((opt.debug>1) && (opt.log!=NULL)) {
  106.         fspc(opt.log,"debug"); fprintf(opt.log,"note: this file is a javascript file"LF); test_flush;
  107.       }
  108.     } else if (strfield2(r.contenttype,"audio/x-pn-realaudio")!=0) {      /* realaudio link file */
  109.       inscript=intag=1;
  110.       intag_start_valid=0;
  111.       in_media="RAM";       // real media!
  112.     }
  113.     // Hack to prevent any problems with ram files of other files
  114.     * ( r.adr + r.size ) = '\0';
  115.  
  116.  
  117.     // ------------------------------------------------------------
  118.     // analyser ce qu'il y a en mΘmoire (fichier html)
  119.     // on scanne les balises
  120.     // ------------------------------------------------------------
  121. #if HTS_ANALYSTE==2
  122.     _hts_in_html_done=0;     // 0% scannΘs
  123.     _hts_cancel=0;           // pas de cancel
  124.     _hts_in_html_parsing=1;  // flag pour indiquer un parsing
  125. #endif
  126.     base[0]='\0';    // effacer base-href
  127.     lastsaved=adr;
  128.     do {
  129.       int p=0;
  130.       int valid_p=0;      // force to take p even if == 0
  131.       error=0;
  132.  
  133.       /* Hack to avoid NULL char problems with C syntax */
  134.       /* Yes, some bogus HTML pages can embed null chars
  135.          and therefore can not be properly handled if this hack is not done
  136.       */
  137.       if ( ! (*adr) ) {
  138.         if (( ((int) adr) - ((int) r.adr) ) < r.size)
  139.           *adr=' ';
  140.       }
  141.  
  142.  
  143.       /* Parse */
  144.       if (
  145.            (*adr=='<')    /* No starting tag */
  146.         && (!inscript)    /* Not in (java)script */
  147.         && (!incomment)   /* Not in comment (<!--) */
  148.       ) { 
  149.         intag=1;
  150.         parseall_incomment=0;
  151.         //inquote=0;  // effacer quote
  152.         intag_start=adr; intag_start_valid=1;
  153.         codebase[0]='\0';    // effacer Θventuel codebase
  154.         
  155.         if (opt.getmode & 1) {  // sauver html
  156.           p=strfield(adr,"</html");
  157.           if (p==0) p=strfield(adr,"<head>");
  158.           if (p) {
  159.             if (strnotempty(opt.footer)) {
  160.               char tempo[1024+HTS_URLMAXSIZE*2];
  161.               char gmttime[256];
  162.               tempo[0]='\0';
  163.               time_gmt_rfc822(gmttime);
  164.               strcpy(tempo,CRLF);
  165.               sprintf(tempo+strlen(tempo),opt.footer,jump_identification(urladr),urlfil,gmttime,"","","","","");
  166.               strcat(tempo,CRLF);
  167.               //fwrite(tempo,1,strlen(tempo),fp);
  168.               HT_ADD(tempo);
  169.             }
  170.           }
  171.         }        
  172.         
  173.         // Θliminer les <!-- (commentaires) : intag dΘvalidΘ
  174.         if (*(adr+1)=='!')
  175.           if (*(adr+2)=='-')
  176.             if (*(adr+3)=='-') {
  177.               intag=0;
  178.               incomment=1;
  179.               intag_start_valid=0;
  180.             }
  181.             
  182.       }
  183.       else if (
  184.            (*adr=='>')                        /* ending tag */
  185.         && ( (!inscript) || (inscript_tag) )  /* and in tag (or in script) */
  186.       ) {
  187.         if (inscript_tag) {
  188.           inscript_tag=inscript=0;
  189.           intag=0;
  190.           incomment=0;
  191.           intag_start_valid=0;
  192.         } else if (!incomment) {
  193.           intag=0; //inquote=0;
  194.           
  195.           // entrΘe dans du javascript?
  196.           // on parse ICI car il se peut qu'on ait eu a parser les src=.. dedans
  197.           //if (!inscript) {  // sinon on est dans un obj.write("..
  198.           if ((intag_start_valid) && check_tag(intag_start,"script")) {
  199.             char* a=intag_start;    // <
  200.             // ** while(is_realspace(*(--a)));
  201.             if (*a=='<') {  // s√r que c'est un tag?
  202.               inscript=1;
  203.               intag=1;     // because aprΦs <script> on y est .. - pas utile
  204.               intag_start_valid=0;    // OUI car nous sommes dans du code, plus dans du "vrai" tag
  205.             }
  206.           }
  207.         } else {                               /* end of comment? */
  208.           // vΘrifier fermeture correcte
  209.           if ( (*(adr-1)=='-') && (*(adr-2)=='-') ) {
  210.             intag=0;
  211.             incomment=0;
  212.             intag_start_valid=0;
  213.           }
  214. #if GT_ENDS_COMMENT
  215.           /* wrong comment ending */
  216.           else {
  217.             /* check if correct ending do not exist 
  218.                <!-- foo > example <!-- bar > is sometimes accepted by browsers
  219.                when no --> is used somewhere else.. darn those browsers are dirty
  220.             */
  221.             if (!strstr(adr,"-->")) {
  222.               intag=0;
  223.               incomment=0;
  224.               intag_start_valid=0;
  225.             }
  226.           }
  227. #endif
  228.         }
  229.         //}
  230.       }
  231.       //else if (*adr==34) {
  232.       //  inquote=(inquote?0:1);
  233.       //}
  234.       else if (intag || inscript) {    // nous sommes dans un tag/commentaire, tester si on recoit un tag
  235.         int p_type=0;
  236.         int p_nocatch=0;
  237.         int p_searchMETAURL=0;  // chercher ..URL=<url>
  238.         int add_class=0;        // ajouter .class
  239.         char* p_flush=NULL;
  240.         
  241.         
  242.         // ------------------------------------------------------------
  243.         // parsing ΘvolΘ
  244.         // ------------------------------------------------------------
  245.         if (((isalpha((unsigned char)*adr)) || (*adr=='/') || (inscript) || (inscriptgen))) {  // sinon pas la peine de tester..
  246.  
  247.  
  248.           /* caractΦre de terminaison pour "miniparsing" javascript=.. ? 
  249.              (ex: <a href="javascript:()" action="foo"> ) */
  250.           if (inscript_tag) {
  251.             if (inscript_tag_lastc) {
  252.               if (*adr == inscript_tag_lastc) {
  253.                 /* sortir */
  254.                 inscript_tag=inscript=0;
  255.                 incomment=0;
  256.               }
  257.             }
  258.           }
  259.  
  260.           // Construction index.html (sommaire)
  261.           // Avant de tester les a href,
  262.           // Ici on teste si l'on doit construire l'index vers le(s) site(s) miroir(s)
  263.           if (!in_media) {
  264.             if (opt.makeindex && (ptr>0)) {
  265.               if (opt.getmode & 1) {  // autorisation d'Θcrire
  266.                 if (!makeindex_done) {  // autoriation d'Θcrire un index
  267.                   if (opt.depth == liens[ptr]->depth) {    // on note toujours les premiers liens
  268.                     if (!detect_title) {
  269.                       p=strfield(adr,"title");  
  270.                       if (p) {
  271.                         if (*(adr-1)=='/') p=0;    // /title
  272.                       } else {
  273.                         if (strfield(adr,"/html"))
  274.                           p=-1;                    // noter, mais sans titre
  275.                         else if (strfield(adr,"body"))
  276.                           p=-1;                    // noter, mais sans titre
  277.                       }
  278.                     } else
  279.                       p=0;
  280.                     
  281.                     if (p) {    // ok center                            
  282.                       if (makeindex_fp==NULL) {
  283.                         verif_backblue(opt.path_html);    // gΘnΘrer gif
  284.                         makeindex_fp=filecreate(fconcat(opt.path_html,"index.html"));
  285.                         if (makeindex_fp!=NULL) {
  286.                           fprintf(makeindex_fp,"<HTML>"CRLF);
  287.                           fprintf(makeindex_fp,"<!-- Mirror and index made by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"CRLF);
  288.                           fprintf(makeindex_fp,"<HEAD>"CRLF"<TITLE>");
  289.                           fprintf(makeindex_fp,"Local index");
  290.                           fprintf(makeindex_fp,"</TITLE>"CRLF"</HEAD>"CRLF"<BODY BACKGROUND=\"backblue.gif\"><H1 ALIGN=Center>");
  291.                           fprintf(makeindex_fp,"<U>Index of locally available sites:</U>"CRLF"</H1>"CRLF"<BR><BR>"CRLF);
  292.                           fprintf(makeindex_fp,"<TABLE BORDER=\"0\" WIDTH=\"100%%\" CELLSPACING=\"1\" CELLPADDING=\"0\">"CRLF);
  293.                           fprintf(makeindex_fp,"<UL>"CRLF);
  294.                         } else makeindex_done=-1;    // fait, erreur
  295.                       }
  296.                       
  297.                       if (makeindex_fp!=NULL) {
  298.                         char tempo[HTS_URLMAXSIZE*2];
  299.                         char s[HTS_URLMAXSIZE*2];
  300.                         char* a=NULL;
  301.                         char* b=NULL;
  302.                         s[0]='\0';
  303.                         if (p>0) {
  304.                           a=strchr(adr,'>');
  305.                           if (a!=NULL) {
  306.                             a++;
  307.                             while(is_space(*a)) a++;    // sauter espaces & co
  308.                             b=strchr(a,'<');   // prochain tag
  309.                           }
  310.                         }
  311.                         if (lienrelatif(tempo,liens[ptr]->sav,concat(opt.path_html,"index.html"))==0) {
  312.                           detect_title=1;      // ok dΘtectΘ pour cette page!
  313.                           makeindex_links++;   // un de plus
  314.                           strcpy(makeindex_firstlink,tempo);
  315.                           //
  316.                           fprintf(makeindex_fp,"<TR>"CRLF"<TD BACKGROUND=\"fade.gif\">"CRLF);
  317.                           //fprintf(makeindex_fp,"<UL>"CRLF);
  318.                           fprintf(makeindex_fp,"<LI>"CRLF);
  319.                           if ((b==a) || (a==NULL) || (b==NULL)) {    // pas de titre
  320.                             fprintf(makeindex_fp,"<A HREF=\"%s\">%s</A><BR>"CRLF,tempo,tempo);
  321.                           } else if ((b-a)<256) {
  322.                             b--;
  323.                             while(is_space(*b)) b--;
  324.                             strncpy(s,a,b-a+1);
  325.                             *(s+(b-a)+1)='\0';
  326.                             fprintf(makeindex_fp,"<A HREF=\"%s\">%s</A><BR>"CRLF,tempo,s);
  327.                           }
  328.                           fprintf(makeindex_fp,"</LI>"CRLF);
  329.                           //fprintf(makeindex_fp,"</UL>"CRLF);
  330.                           fprintf(makeindex_fp,"</TD>"CRLF"</TR>"CRLF);
  331.                         }
  332.                       }
  333.                     }
  334.                     
  335.                   } else if (liens[ptr]->depth<opt.depth) {   // on a sautΘ level1+1 et level1
  336.                     if (makeindex_fp) {
  337.                       fprintf(makeindex_fp,"</UL>"CRLF);
  338.                       fprintf(makeindex_fp,"</TABLE>"CRLF);
  339.                       fprintf(makeindex_fp,"<BR>"CRLF"<BR>"CRLF"<BR>"CRLF"</BODY>"CRLF);
  340.                       fprintf(makeindex_fp,"<I><H6 ALIGN=\"RIGHT\">Mirror and index made by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS"</H6></I>"CRLF);
  341.                       fprintf(makeindex_fp,"<!-- Mirror and index made by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"CRLF);
  342.                       fprintf(makeindex_fp,"<!-- Thanks for using HTTrack Website Copier! -->"CRLF);
  343.                       if (makeindex_links == 1) {
  344.                         fprintf(makeindex_fp,"<meta HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=%s\">"CRLF,makeindex_firstlink);
  345.                       }
  346.                       fprintf(makeindex_fp,"</HTML>"CRLF);
  347.                       fflush(makeindex_fp);
  348.                       fclose(makeindex_fp);  // α ne pas oublier sinon on passe une nuit blanche
  349.                       makeindex_fp=NULL;
  350.                       usercommand(0,NULL,fconcat(opt.path_html,"index.html"));                            
  351.                     }
  352.                     makeindex_done=1;    // ok c'est fait
  353.                   }
  354.                 }
  355.               }
  356.             } // if (opt.makeindex)
  357.           }
  358.           // FIN Construction index.html (sommaire)
  359.           
  360.           
  361.           // Note:
  362.           // Certaines pages ne respectent pas le html
  363.           // notamment les guillements ne sont pas fixΘs
  364.           // Nous sommes dans un tag, donc on peut faire un test plus
  365.           // large pour pouvoi prendre en compte ces particularitΘs
  366.           
  367.           // α vΘrifier: ACTION, CODEBASE, VRML
  368.           
  369.           if (in_media) {
  370.             if (strcmp(in_media,"RAM")==0) { // real media
  371.               p=0;
  372.               valid_p=1;
  373.             }
  374.           } else if (ptr>0) {        /* pas premiΦre page 0 (primary) */
  375.             p=0;  // saut pour le nom de fichier: adresse nom fichier=adr+p
  376.             
  377.             // ------------------------------
  378.             // dΘtection d'Θcriture JavaScript.
  379.             // osons les obj.write et les obj.href=.. ! osons!
  380.             // note: inscript==1 donc on sautera aprΦs les \"
  381.             if (inscript) {
  382.               if (inscriptgen) {          // on est dΘja dans un objet gΘnΘrant..
  383.                 if (*adr==scriptgen_q) {  // fermeture des " ou '
  384.                   if (*(adr-1)!='\\') {   // non
  385.                     inscriptgen=0;        // ok parsing terminΘ
  386.                   }
  387.                 }
  388.               } else {
  389.                 char* a=NULL;
  390.                 char check_this_fking_line=0;  // parsing code javascript..
  391.                 char must_be_terminated=0;     // caractΦre obligatoire de terminaison!
  392.                 int token_size;
  393.                 if (!(token_size=strfield(adr,".writeln"))) // dΘtection ...objet.write[ln]("code html")...
  394.                   token_size=strfield(adr,".write");
  395.                 if (token_size) {
  396.                   a=adr+token_size;
  397.                   while(is_realspace(*a)) a++; // sauter espaces
  398.                   if (*a=='(') {  // dΘbut parenthΦse
  399.                     check_this_fking_line=2;  // α parser!
  400.                     must_be_terminated=')';
  401.                     a++;  // sauter (
  402.                   }
  403.                 }
  404.                 // euhh ??? ???
  405.                 /* else if (strfield(adr,".href")) {  // dΘtection ...objet.href="...
  406.                 a=adr+5;
  407.                 while(is_realspace(*a)) a++; // sauter espaces
  408.                 if (*a=='=') {  // ohh un Θgal
  409.                 check_this_fking_line=1;  // α noter!
  410.                 must_be_terminated=';';   // et si t'as oubliΘ le ; tu sais pas coder
  411.                 a++;   // sauter =
  412.                 }
  413.                 
  414.               }*/
  415.                 
  416.                 // on a un truc du genre instruction"code gΘnΘrΘ" dont on parse le code
  417.                 if (check_this_fking_line) {
  418.                   while(is_realspace(*a)) a++;
  419.                   if ((*a=='\'') || (*a=='"')) {  // dΘpart de '' ou ""
  420.                     char *b;
  421.                     int ex=0;
  422.                     scriptgen_q=*a;    // quote
  423.                     b=a+1;      // dΘpart de la chaεne
  424.                     // vΘrifier forme ("code") et pas ("code"+var), ingΘrable
  425.                     do {
  426.                       a++;  // caractΦre suivant
  427.                       if (*a==scriptgen_q) if (*(a-1)!='\\')  // quote non slash
  428.                         ex=1;            // sortie
  429.                       if ((*a==10) || (*a==13))
  430.                         ex=1;
  431.                     } while(!ex);
  432.                     if (*a==scriptgen_q) {  // fin du quote
  433.                       a++;
  434.                       while(is_realspace(*a)) a++;
  435.                       if (*a==must_be_terminated) {  // parenthΦse fermante: ("..")
  436.                         
  437.                         // bon, on doit parser une ligne javascript
  438.                         // 1) si check.. ==1 alors c'est un nom de fichier direct, donc
  439.                         // on fixe p sur le saut nΘcessaire pour atteindre le nom du fichier
  440.                         // et le moteur se dΘbrouillera ensuite tout seul comme un grand
  441.                         // 2) si check==2 c'est un peu plus tordu car lα on gΘnΘre du
  442.                         // code html au sein de code javascript au sein de code html
  443.                         // dans ce cas on doit fixer un flag α un puis ensuite dans la boucle
  444.                         // on devra parser les instructions standard comme <a href etc
  445.                         // NOTE: le code javascript autogΘnΘrΘ n'est pas pris en compte!!
  446.                         // (et ne marche pas dans 50% des cas de toute facon!)
  447.                         if (check_this_fking_line==1) {
  448.                           p=(int) b-(int) adr;  // calculer saut!
  449.                         } else {
  450.                           inscriptgen=1;        // SCRIPTGEN actif
  451.                           adr=b;                // jump
  452.                         }
  453.                         
  454.                         if ((opt.debug>1) && (opt.log!=NULL)) {
  455.                           char str[512];
  456.                           str[0]='\0';
  457.                           strncat(str,b,minimum((int) a-(int) b+1,32));
  458.                           fspc(opt.log,"debug"); fprintf(opt.log,"active code (%s) detected in javascript: %s"LF,(check_this_fking_line==2)?"parse":"pickup",str); test_flush;
  459.                         }
  460.                       }
  461.                       
  462.                     }
  463.                     
  464.                   }
  465.                   
  466.                   
  467.                 }
  468.               }
  469.             }
  470.             // fin detection code gΘnΘrant javascript vers html
  471.             // ------------------------------
  472.             
  473.             
  474.             // analyse proprement dite, A HREF=.. etc..
  475.             if (!p) {
  476.               // si dans un tag, et pas dans un script - sauf si on analyse un obj.write("..
  477.               if ((intag && (!inscript)) || inscriptgen) {
  478.                 if ( (*(adr-1)=='<') || (is_space(*(adr-1))) ) {   // <tag < tag etc
  479.                   // <A HREF=.. pour les liens HTML
  480.                   p=rech_tageq(adr,"href");
  481.                   if (p) {    // href.. tester si c'est une bas href!
  482.                     if ((intag_start_valid) && check_tag(intag_start,"base")) {  // oui!
  483.                       // ** note: base href et codebase ne font pas bon mΘnage..
  484.                       p_type=2;    // c'est un chemin
  485.                     }
  486.                   }
  487.                   
  488.                   /* Tags supplΘmentaires α vΘrifier (<img src=..> etc) */
  489.                   if (p==0) {
  490.                     int i=0;
  491.                     while( (p==0) && (strnotempty(hts_detect[i])) ) {
  492.                       p=rech_tageq(adr,hts_detect[i]);
  493.                       i++;
  494.                     }
  495.                   }
  496.  
  497.                   /* Tags supplΘmentaires α vΘrifier : URL=.. */
  498.                   if (p==0) {
  499.                     int i=0;
  500.                     while( (p==0) && (strnotempty(hts_detectURL[i])) ) {
  501.                       p=rech_tageq(adr,hts_detectURL[i]);
  502.                       i++;
  503.                     }
  504.                     if (p)
  505.                       p_searchMETAURL=1;
  506.                   }
  507.  
  508.                   /* Tags supplΘmentaires α vΘrifier, mais α ne pas capturer */
  509.                   if (p==0) {
  510.                     int i=0;
  511.                     while( (p==0) && (strnotempty(hts_detectandleave[i])) ) {
  512.                       p=rech_tageq(adr,hts_detectandleave[i]);
  513.                       i++;
  514.                     }
  515.                     if (p)
  516.                       p_nocatch=1;      /* ne pas rechercher */
  517.                   }
  518.  
  519.                   /* EvΘnements */
  520.                   if (p==0) {
  521.                     int i=0;
  522.                     /* dΘtection onLoad etc */
  523.                     while( (p==0) && (strnotempty(hts_detect_js[i])) ) {
  524.                       p=rech_tageq(adr,hts_detect_js[i]);
  525.                       i++;
  526.                     }
  527.                     /* non dΘtectΘ - dΘtecter Θgalement les onXxxxx= */
  528.                     if (p==0) {
  529.                       if ( (*adr=='o') && (*(adr+1)=='n') && isUpperLetter(*(adr+2)) ) {
  530.                         p=0;
  531.                         while(isalpha((unsigned char)adr[p]) && (p<64) ) p++;
  532.                         if (p<64) {
  533.                           while(is_space(adr[p])) p++;
  534.                           if (adr[p]=='=')
  535.                             p++;
  536.                           else p=0;
  537.                         } else p=0;
  538.                       }
  539.                     }
  540.                     /* OK, ΘvΘnement repΘrΘ */
  541.                     if (p) {
  542.                       inscript_tag_lastc=*(adr+p-1);     /* α attendre α la fin */
  543.                       adr+=p;     /* saut */
  544.                                   /*
  545.                                   On est dΘsormais dans du code javascript
  546.                       */
  547.                       inscript_tag=inscript=1;
  548.                     }
  549.                     p=0;        /* quoi qu'il arrive, ne rien dΘmarrer ici */
  550.                   }
  551.  
  552.                   // <APPLET CODE=.. pour les applet java.. [CODEBASE (chemin..) α faire]
  553.                   if (p==0) {
  554.                     p=rech_tageq(adr,"code");
  555.                     if (p) {
  556.                       if ((intag_start_valid) && check_tag(intag_start,"applet")) {  // dans un <applet !
  557.                         p_type=-1;  // juste le nom de fichier+dossier, Θcire avant codebase 
  558.                         add_class=1;   // ajouter .class au besoin                         
  559.                         
  560.                         // vΘrifier qu'il n'y a pas de codebase APRES
  561.                         // sinon on swappe les deux.
  562.                         // pas trΦs propre mais c'est ce qu'il y a de plus simple α faire!!
  563.                         
  564.                         {
  565.                           char *a;
  566.                           a=adr;
  567.                           while((*a) && (*a!='>') && (!rech_tageq(a,"codebase"))) a++;
  568.                           if (rech_tageq(a,"codebase")) {  // banzai! codebase=
  569.                             char* b;
  570.                             b=strchr(a,'>');
  571.                             if (b) {
  572.                               if (((int) b - (int) adr) < 1000) {    // au total < 1Ko
  573.                                 char tempo[HTS_URLMAXSIZE*2];
  574.                                 tempo[0]='\0';
  575.                                 strncat(tempo,a,(int) b - (int) a);
  576.                                 strcat( tempo," ");
  577.                                 strncat(tempo,adr,(int) a - (int) adr - 1);
  578.                                 // Θventuellement remplire par des espaces pour avoir juste la taille
  579.                                 while((int) strlen(tempo)<((int) b - (int) adr))
  580.                                   strcat(tempo," ");
  581.                                 // pas d'erreur?
  582.                                 if ((int) strlen(tempo) == ((int) b - (int) adr)) {
  583.                                   strncpy(adr,tempo,strlen(tempo));   // PAS d'octet nul α la fin!
  584.                                   p=0;    // DEVALIDER!!
  585.                                   p_type=0;
  586.                                   add_class=0;
  587.                                 }
  588.                               }
  589.                             }
  590.                           }
  591.                         }
  592.                         
  593.                       }
  594.                     }
  595.                   }
  596.                   
  597.                   // liens α patcher mais pas α charger (ex: codebase)
  598.                   if (p==0) {  // note: si non chargΘ (ex: ignorer .class) patchΘ tout de mΩme
  599.                     p=rech_tageq(adr,"codebase");
  600.                     if (p) {
  601.                       if ((intag_start_valid) && check_tag(intag_start,"applet")) {  // dans un <applet !
  602.                         p_type=-2;
  603.                       } else p=-1;   // ne plus chercher
  604.                     }
  605.                   }
  606.                   
  607.                   
  608.                   // Meta tags pour robots
  609.                   if (p==0) {
  610.                     if (opt.robots) {
  611.                       if ((intag_start_valid) && check_tag(intag_start,"meta")) {
  612.                         if (rech_tageq(adr,"name")) {    // name=robots.txt
  613.                           char tempo[1100];
  614.                           char* a;
  615.                           tempo[0]='\0';
  616.                           a=strchr(adr,'>');
  617. #if DEBUG_ROBOTS
  618.                           printf("robots.txt meta tag detected\n");
  619. #endif
  620.                           if (a) {
  621.                             if (((int) a - (int) adr) < 999 ) {
  622.                               strncat(tempo,adr,(int) a - (int) adr);
  623.                               if (strstrcase(tempo,"content")) {
  624.                                 if (strstrcase(tempo,"robots")) {
  625.                                   if (strstrcase(tempo,"nofollow")) {
  626. #if DEBUG_ROBOTS
  627.                                     printf("robots.txt meta tag: nofollow in %s%s\n",urladr,urlfil);
  628. #endif
  629.                                     nofollow=1;       // NE PLUS suivre liens dans cette page
  630.                                     if (opt.errlog) {
  631.                                       fspc(opt.errlog,"warning"); fprintf(opt.errlog,"Link %s%s not scanned (follow robots meta tag)"LF,urladr,urlfil);
  632.                                       test_flush;
  633.                                     }
  634.                                   }
  635.                                 }
  636.                               }
  637.                             }
  638.                           }
  639.                         }
  640.                       }
  641.                     }
  642.                   }
  643.                   
  644.                   // entrΘe dans une applet javascript
  645.                   /*if (!inscript) {  // sinon on est dans un obj.write("..
  646.                   if (p==0)
  647.                   if (rech_sampletag(adr,"script"))
  648.                   if (check_tag(intag_start,"script")) {
  649.                   inscript=1;
  650.                   }
  651.                         }*/
  652.                   
  653.                   // Ici on procΦde α une analyse du code javascript pour tenter de rΘcupΘrer
  654.                   // certains fichiers Θvidents.
  655.                   // C'est devenu obligatoire vu le nombre de pages qui intΦgrent
  656.                   // des images rΘactives par exemple
  657.                 }
  658.               } else if (inscript) {
  659.                 if (strfield(adr,"/script") ) {
  660.                   char* a=adr;
  661.                   //while(is_realspace(*(--a)));
  662.                   while( is_realspace(*a) ) a--;
  663.                   a--;
  664.                   if (*a=='<') {  // s√r que c'est un tag?
  665.                     inscript=0;
  666.                   }
  667.                 } else {
  668.                   int nc;
  669.                   char  expected     = '=';          // caractΦre attendu aprΦs
  670.                   char* expected_end = ";";
  671.                   if (inscript_tag)
  672.                     expected_end=";\"\'";            // voir a href="javascript:doc.location='foo'"
  673.                   nc = strfield(adr,".src");  // nom.src="image";
  674.                   if (!nc) nc = strfield(adr,".location");  // document.location="doc"
  675.                   if (!nc) nc = strfield(adr,".href");  // document.location="doc"
  676.                   if (!nc) if ( (nc = strfield(adr,".open")) ) { // window.open("doc",..
  677.                     expected='(';    // parenthΦse
  678.                     expected_end="),";  // fin: virgule ou parenthΦse
  679.                   }
  680.                   if (!nc) if ( (nc = strfield(adr,".replace")) ) { // window.replace("url")
  681.                     expected='(';    // parenthΦse
  682.                     expected_end=")";  // fin: parenthΦse
  683.                   }
  684.                   if (!nc) if ( (nc = strfield(adr,".link")) ) { // window.link("url")
  685.                     expected='(';    // parenthΦse
  686.                     expected_end=")";  // fin: parenthΦse
  687.                   }
  688.                   if (nc) {
  689.                     char *a;
  690.                     a=adr+nc;
  691.                     while(is_space(*a)) a++;
  692.                     if (*a == expected) {
  693.                       a++;
  694.                       while(is_realspace(*a)) a++;
  695.                       if ((*a==34) || (*a=='\'')) {
  696.                         char *b,*c;
  697.                         a++;
  698.                         b=a;
  699.                         while((*b!=34) && (*b!='\'') && (*b!='\0')) b++;
  700.                         c=b--; c++;
  701.                         while(*c==' ') c++;
  702.                         if ((strchr(expected_end,*c)) || (*c=='\n') || (*c=='\r')) {
  703.                           c-=2;
  704.                           if ((int) c-(int) a+1) {
  705.                             if ((opt.debug>1) && (opt.log!=NULL)) {
  706.                               char str[512];
  707.                               str[0]='\0';
  708.                               strncat(str,a,minimum((int) c-(int) a+1,32));
  709.                               fspc(opt.log,"debug"); fprintf(opt.log,"link detected in javascript: %s"LF,str); test_flush;
  710.                             }
  711.                             p=(int) a- (int) adr;    // p non nul: TRAITER CHAINE COMME FICHIER
  712.                           }
  713.                         }
  714.                         
  715.                         
  716.                       }
  717.                     }
  718.                   }
  719.                   
  720.                 }
  721.               }
  722.             }
  723.                 
  724.           } else
  725.             p=rech_tageq(adr,"primary");    // lien primaire, yeah
  726.           }
  727.           
  728.           
  729.           // ------------------------------------------------------------
  730.           // dernier recours - parsing "sale" : dΘtection systΘmatique des .gif, etc.
  731.           // risque: gΘnΘrer de faux fichiers parazites
  732.           // fix: ne parse plus dans les commentaires
  733.           // ------------------------------------------------------------
  734.           if ( (opt.parseall) && (ptr>0) && (!in_media) ) {           // option parsing "brut"
  735.             int incomment_justquit=0;
  736.             if (!is_realspace(*adr)) {
  737.               int noparse=0;
  738.  
  739.               // Gestion des /* */
  740.               if (inscript) {
  741.                 if (parseall_incomment) {
  742.                   if ((*adr=='/') && (*(adr-1)=='*'))
  743.                     parseall_incomment=0;
  744.                   incomment_justquit=1;       // ne pas noter dernier caractΦre
  745.                 } else {
  746.                   if ((*adr=='/') && (*(adr+1)=='*'))
  747.                     parseall_incomment=1;
  748.                 }
  749.               } else
  750.                 parseall_incomment=0;
  751.  
  752.               /* vΘrifier que l'on est pas dans un <!-- --> pur */
  753.               if ( (!intag) && (incomment) && (!inscript))
  754.                 noparse=1;        /* commentaire */
  755.  
  756.               // recherche d'URLs
  757.               if ((!parseall_incomment) && (!noparse)) {
  758.                 if (!p) {                   // non dΘja trouvΘ
  759.                   if (adr != r.adr) {     // >1 caractΦre
  760.                     // scanner les chaines
  761.                     if ((*adr == '\"') || (*adr=='\'')) {         // "xx.gif" 'xx.gif'
  762.                       if (strchr("=(,",parseall_lastc)) {    // exemple: a="img.gif..
  763.                         char *a=adr;
  764.                         char stop=*adr;  // " ou '
  765.                         int count=0;
  766.                         
  767.                         // sauter caractΦres
  768.                         a++;
  769.                         while((*a) && (*a!='\'') && (*a!='\"') && (count<HTS_URLMAXSIZE)) { count++; a++; }
  770.                         
  771.                         // ok chaine terminΘe par " ou '
  772.                         if ((*a == stop) && (count<HTS_URLMAXSIZE) && (count>0)) {
  773.                           char c;
  774.                           char* aend;
  775.                           //
  776.                           aend=a;     // sauver dΘbut
  777.                           a++;
  778.                           while(is_realspace(*a)) a++;
  779.                           c=*a;
  780.                           if (strchr("),;>/+",c)) {     // exemple: ..img.gif";
  781.                             // le / est pour funct("img.gif" /* URL */);
  782.                             char tempo[HTS_URLMAXSIZE*2];
  783.                             char type[256];
  784.                             int url_ok=0;      // url valide?
  785.                             tempo[0]='\0'; type[0]='\0';
  786.                             //
  787.                             strncat(tempo,adr+1,count);
  788.                             //
  789.                             if ((!strchr(tempo,' ')) || inscript) {   // espace dedans: mΘfiance! (sauf dans code javascript)
  790.                               int invalid_url=0;
  791.                               
  792.                               // vΘrifier qu'il n'y a pas de caractΦres spΘciaux
  793.                               if (strchr(tempo,'*')
  794.                                 || strchr(tempo,'<')
  795.                                 || strchr(tempo,'>'))
  796.                                 invalid_url=1;
  797.                               
  798.                               /* non invalide? */
  799.                               if (!invalid_url) {
  800.                                 // Un plus α la fin? Alors ne pas prendre sauf si extension ("/toto.html#"+tag)
  801.                                 if (c!='+') {    // PAS de plus α la fin
  802.                                   char* a;
  803.                                   if ((strncmp(tempo,"http://",7)==0) || (strncmp(tempo,"ftp://",6)==0))  // ok pas de problΦme
  804.                                     url_ok=1;
  805.                                   else if (tempo[strlen(tempo)-1]=='/') {        // un slash: ok..
  806.                                     if (inscript)   // sinon si pas javascript, mΘfiance (rΘpertoire style base?)
  807.                                       url_ok=1;
  808.                                   } else if ((a=strchr(tempo,'/'))) {        // un slash: ok..
  809.                                     if (inscript) {    // sinon si pas javascript, mΘfiance (style "text/css")
  810.                                       if (strchr(a+1,'/'))  // un seul / : abandon (STYLE type='text/css')
  811.                                         url_ok=1;
  812.                                     }
  813.                                   }
  814.                                 }
  815.                                 // Prendre si extension reconnue
  816.                                 if (!url_ok) {
  817.                                   get_httptype(type,tempo,0);
  818.                                   if (strnotempty(type))     // type reconnu!
  819.                                     url_ok=1;
  820.                                 }
  821.                                 //
  822.                                 // Ok, cela pourrait Ωtre une URL
  823.                                 if (url_ok) {
  824.                                   // Accepter URL, on la traitera comme une URL normale!!
  825.                                   p=1;
  826.                                 }
  827.                               }
  828.                             }
  829.                           }
  830.                         }
  831.                       }
  832.                     }
  833.                   }
  834.                 }  // p == 0
  835.                 
  836.                 // plus dans un commentaire
  837.                 if (!incomment_justquit)
  838.                   parseall_lastc=*adr;             // caractΦre avant le prochain
  839.                 
  840.               } // not in comment
  841.               
  842.             }  // if realspace
  843.           }  // if parseall
  844.           
  845.           
  846.           // ------------------------------------------------------------
  847.           // p!=0 : on a repΘrΘ un Θventuel lien
  848.           // ------------------------------------------------------------
  849.           //
  850.           if ((p>0) || (valid_p)) {    // on a repΘrΘ un lien
  851.             //int lien_valide=0;
  852.             char* eadr=NULL;          /* fin de l'URL */
  853.             char* quote_adr=NULL;     /* adresse du ? dans l'adresse */
  854.             int ok=1;
  855.             char quote='\0';
  856.  
  857.             // TEST
  858.             /*{
  859.               static int loop=0;
  860.               if ((++loop)%5000==0)
  861.                 loop=0;
  862.             }*/
  863.             
  864.             // si nofollow a ΘtΘ dΘclenchΘ, rΘΘcrire tous les liens en externe
  865.             if (nofollow)
  866.               p_nocatch=1;
  867.  
  868.             // Θcrire codebase avant, flusher avant code
  869.             if ((p_type==-1) || (p_type==-2)) {
  870.               if ((opt.getmode & 1) && (ptr>0)) {
  871.                 HT_ADD_ADR;    // refresh
  872.               }
  873.               lastsaved=adr;    // dernier Θcrit+1
  874.             }
  875.             
  876.             // sauter espaces
  877.             adr+=p;
  878.             while((is_space(*adr)) && (quote=='\0')) {
  879.               if (!quote)
  880.                 if ((*adr=='\"') || (*adr=='\''))
  881.                   quote=*adr;                     // on doit attendre cela α la fin
  882.                                                   // puis quitter
  883.                 adr++;    // sauter les espaces, "" et cie
  884.             }
  885.             /* s'arrΩter que ce soit un ' ou un " : pour document.write('<img src="foo'+a); par exemple! */
  886.             if (inscript)
  887.               quote='\0';
  888.             
  889.             // sauter Θventuel \" ou \' javascript
  890.             if (inscript) {    // on est dans un obj.write("..
  891.               if (*adr=='\\') {
  892.                 if ((*(adr+1)=='\'') || (*(adr+1)=='"')) {  // \" ou \'
  893.                   adr+=2;    // sauter
  894.                 }
  895.               }
  896.             }
  897.             
  898.             // sauter content="1;URL=http://..
  899.             if (p_searchMETAURL) {
  900.               int l=0;
  901.               while(!strfield(adr+l,"URL=") && (l<128) ) l++;
  902.               if (!strfield(adr,"URL="))
  903.                 ok=-1;
  904.               else
  905.                 adr+=(l+4);
  906.             }
  907.  
  908.             /* Θviter les javascript:document.location=.. : les parser, plut⌠t */
  909.             if (ok!=-1) {
  910.               if (strfield(adr,"javascript:")) {
  911.                 ok=-1;
  912.                 /*
  913.                 On est dΘsormais dans du code javascript
  914.                 */
  915.                 inscript_tag=inscript=1;
  916.                 inscript_tag_lastc=quote;     /* α attendre α la fin */
  917.               }
  918.             }
  919.             
  920.             if (p_type==1) {
  921.               if (*adr=='#') {
  922.                 adr++;           // sauter # pour usemap etc
  923.               }
  924.             }
  925.             eadr=adr;
  926.             
  927.             // ne pas flusher aprΦs code si on doit Θcrire le codebase avant!
  928.             if ((p_type!=-1) && (p_type!=2) && (p_type!=-2)) {
  929.               if ((opt.getmode & 1) && (ptr>0)) {
  930.                 HT_ADD_ADR;    // refresh
  931.               }
  932.               lastsaved=adr;    // dernier Θcrit+1
  933.               // aprΦs on Θcrira soit les donnΘes initiales,
  934.               // soir une URL/lien modifiΘ!
  935.             } else if (p_type==-1) p_flush=adr;    // flusher jusqu'α adr ensuite
  936.             
  937.             if (ok!=-1) {    // continuer
  938.               // dΘcouper le lien
  939.               do {
  940.                 if ((* (unsigned char*) eadr)<32) {   // caractΦre de contr⌠le (ou \0)
  941.                   if (!is_space(*eadr))
  942.                     ok=0; 
  943.                 }
  944.                 if ( ( ((int) eadr) - ((int) adr) ) > HTS_URLMAXSIZE)  // ** trop long, >HTS_URLMAXSIZE caractΦres (on prΘvoit HTS_URLMAXSIZE autres pour path)
  945.                   ok=-1;    // ne pas traiter ce lien
  946.                 
  947.                 if (ok) {
  948.                   //if (*eadr!=' ') {  
  949.                   if (is_space(*eadr)) {   // guillemets,CR, etc
  950.                     if ((!quote) || (*eadr==quote))     // si pas d'attente de quote spΘciale ou si quote atteinte
  951.                       ok=0; 
  952.                   }
  953.                   else {
  954.                     switch(*eadr) {
  955.                     case '>': 
  956.                       if (!quote) {
  957.                         if (!inscript) {
  958.                           intag=0;    // PLUS dans un tag!
  959.                           intag_start_valid=0;
  960.                         }
  961.                         ok=0;
  962.                       }
  963.                       break;
  964.                       /*case '<':*/ case '#': ok=0; break;    // case '?': non!
  965.                     case '\\': if (inscript) ok=0; break;     // \" ou \' point d'arrΩt
  966.                     case '?': quote_adr=adr; break;           // noter position query
  967.                     }
  968.                   }
  969.                   //}
  970.                 } 
  971.                 eadr++;
  972.               } while(ok==1);     
  973.               
  974.               if ( (((int) eadr)-((int) adr)) <= 1) ok=-1;     // lien vide
  975.             }
  976.             
  977.             if (ok==0) {    // tester un lien
  978.               char lien[HTS_URLMAXSIZE*2];
  979.               int meme_adresse=0;      // 0 par dΘfaut pour primary
  980.               //char *copie_de_adr=adr;
  981.               //char* p;
  982.               
  983.               // construire lien (dΘcoupage)
  984.               if ( (((int) eadr)-((int) adr)-1) < HTS_URLMAXSIZE  ) {    // pas trop long?
  985.                 strncpy(lien,adr,((int) eadr)-((int) adr)-1);
  986.                 *(lien+  (((int) eadr)-((int) adr))-1  )='\0';
  987.                 //printf("link: %s\n",lien);          
  988.                 // supprimer les espaces
  989.                 while((lien[strlen(lien)-1]==' ') && (strnotempty(lien))) lien[strlen(lien)-1]='\0';
  990.                 // supprimer les // en / (sauf pour http://)
  991.                 {
  992.                   char *a,*p,*q;
  993.                   int done=0;
  994.                   a=strstr(lien,":/");    // http://
  995.                   if (a) {
  996.                     a++;
  997.                     while(*a=='/') a++;    // position aprΦs http://
  998.                   } else {
  999.                     a=lien;                // dΘbut
  1000.                   }
  1001.                   q=strchr(a,'?');     // ne pas traiter aprΦs '?'
  1002.                   if (!q)
  1003.                     q=a+strlen(a)-1;
  1004.                   while(( p=strstr(a,"//")) && (!done) ) {    // remplacer // par /
  1005.                     if ((int) p>(int) q) {   // aprΦs le ? (toto.cgi?param=1//2.3)
  1006.                       done=1;    // stopper
  1007.                     } else {
  1008.                       char tempo[HTS_URLMAXSIZE*2];
  1009.                       tempo[0]='\0';
  1010.                       strncat(tempo,a,(int) p - (int) a);
  1011.                       strcat (tempo,p+1);
  1012.                       strcpy(a,tempo);    // recopier
  1013.                     }
  1014.                   }
  1015.                 }
  1016.               } else
  1017.                 lien[0]='\0';    // erreur
  1018.               
  1019.               // ------------------------------------------------------
  1020.               // Lien repΘrΘ et extrait
  1021.               if (strnotempty(lien)>0) {           // construction du lien
  1022.                 char adr[HTS_URLMAXSIZE*2],fil[HTS_URLMAXSIZE*2];          // ATTENTION adr cache le "vrai" adr
  1023.                 int forbidden_url=-1;              // lien non interdit (mais non autorisΘ..)
  1024.                 int just_test_it=0;                // mode de test des liens
  1025.                 int set_prio_to=0;                 // pour capture de page isolΘe
  1026.                 int import_done=0;                 // lien importΘ (ne pas scanner ensuite *α priori*)
  1027.                 //
  1028.                 adr[0]='\0'; fil[0]='\0';
  1029.                 //
  1030.                 // 0: autorisΘ
  1031.                 // 1: interdit (patcher tout de mΩme adresse)
  1032.                 
  1033.                 if ((opt.debug>1) && (opt.log!=NULL)) {
  1034.                   fspc(opt.log,"debug"); fprintf(opt.log,"link detected in html: %s"LF,lien); test_flush;
  1035.                 }
  1036.  
  1037.                 // purger CR,LF rΘsiduels (IMG SRC="foo.<\n>gif")
  1038.                 {
  1039.                   char* a;
  1040.                   while ((a=strchr(lien,'\n'))) {
  1041.                     char tempo[HTS_URLMAXSIZE*2];
  1042.                     tempo[0]='\0';
  1043.                     strncat(tempo,lien,(int) a - (int) lien);
  1044.                     strcat(tempo,a+1);
  1045.                     strcpy(lien,tempo);
  1046.                   }
  1047.                   while ((a=strchr(lien,'\r'))) {
  1048.                     char tempo[HTS_URLMAXSIZE*2];
  1049.                     tempo[0]='\0';
  1050.                     strncat(tempo,lien,(int) a - (int) lien);
  1051.                     strcat(tempo,a+1);
  1052.                     strcpy(lien,tempo);
  1053.                   }
  1054.                 }
  1055.                 
  1056.                 /* Unescape/escape %20 and other   */
  1057.                 {
  1058.                   char query[HTS_URLMAXSIZE*2];
  1059.                   char* a=strchr(lien,'?');
  1060.                   if (a) {
  1061.                     strcpy(query,a);
  1062.                     *a='\0';
  1063.                   } else
  1064.                     query[0]='\0';
  1065.                   // conversion & -> & et autres joyeusetΘs
  1066.                   unescape_amp(lien);
  1067.                   // dΘcoder l'inutile (%2E par exemple) et coder espaces
  1068.                   strcpy(lien,unescape_http(lien));
  1069.                   escape_spc_url(lien);
  1070.                   strcat(lien,query);     /* restore */
  1071.                 }
  1072.                 
  1073.                 // convertir les Θventuels \ en des / pour Θviter des problΦmes de reconnaissance!
  1074.                 {
  1075.                   char* a=jump_identification(lien);
  1076.                   while( (a=strchr(a,'\\')) ) *a='/';
  1077.                 }
  1078.                 
  1079.                 // supprimer le(s) ./
  1080.                 while ((lien[0]=='.') && (lien[1]=='/')) {
  1081.                   char tempo[HTS_URLMAXSIZE*2];
  1082.                   strcpy(tempo,lien+2);
  1083.                   strcpy(lien,tempo);
  1084.                 }
  1085.                 if (strnotempty(lien)==0)  // sauf si plus de nom de fichier
  1086.                   strcpy(lien,"./");
  1087.                 
  1088.                 // vΘrifie les /~machin -> /~machin/
  1089.                 // supposition dangereuse?
  1090.                 if (lien[strlen(lien)-1]!='/') {
  1091.                   char *a=lien+strlen(lien)-1;
  1092.                   // Θviter aussi index~1.html
  1093.                   while (((int) a>(int) lien) && (*a!='~') && (*a!='/') && (*a!='.')) a--;
  1094.                   if (*a=='~') {
  1095.                     strcat(lien,"/");    // ajouter slash
  1096.                   }
  1097.                 }
  1098.                 
  1099.                 
  1100.                 // Θliminer les Θventuels :80 (port par dΘfaut!)
  1101.                 {
  1102.                   char* a;
  1103.                   a=strstr(lien,"://");
  1104.                   if (a)
  1105.                     a+=3;
  1106.                   else
  1107.                     a=lien;
  1108.                   while((*a) && (*a!='/') && (*a!=':')) a++;
  1109.                   if (*a==':') {  // port
  1110.                     int port=0;
  1111.                     char* b=a+1;
  1112.                     while(isdigit((unsigned char)*b)) { port*=10; port+=(int) (*b-'0'); b++; }
  1113.                     if (port==80) {  // port 80, default
  1114.                       char tempo[HTS_URLMAXSIZE*2];
  1115.                       tempo[0]='\0';
  1116.                       strncat(tempo,lien,(int) a-(int) lien);
  1117.                       strcat(tempo,a+3);  // sauter :80
  1118.                       strcpy(lien,tempo);
  1119.                     }
  1120.                   }
  1121.                 }
  1122.                 
  1123.                 // filtrer les parazites (mailto & cie)
  1124.                 if (strfield(lien,"mailto:")) {  // ne pas traiter
  1125.                   error=1;
  1126.                 } else if (strfield(lien,"news:")) {  // ne pas traiter
  1127.                   error=1;
  1128.                 }
  1129.                 
  1130.                 // vΘrifier que l'on ne doit pas ajouter de .class
  1131.                 if (!error) {
  1132.                   if (add_class) {
  1133.                     char *a = lien+strlen(lien)-1;
  1134.                     while(((int) a > (int) lien) && (*a!='/') && (*a!='.')) a--;
  1135.                     if (*a != '.')
  1136.                       strcat(lien,".class");    // ajouter .class
  1137.                   }
  1138.                 }
  1139.                 
  1140.                 // si c'est un chemin, alors vΘrifier (toto/toto.html -> http://www/toto/)
  1141.                 if (!error) {
  1142.                   if ((opt.debug>1) && (opt.log!=NULL)) {
  1143.                     fspc(opt.log,"debug"); fprintf(opt.log,"position link check %s"LF,lien); test_flush;
  1144.                   }
  1145.                   
  1146.                   if ((p_type==2) || (p_type==-2)) {   // code ou codebase                        
  1147.                     // VΘrifier les codebase=applet (au lieu de applet/)
  1148.                     if (p_type==-2) {    // codebase
  1149.                       if (strnotempty(lien)) {
  1150.                         if (fil[strlen(lien)-1]!='/') {  // pas rΘpertoire
  1151.                           strcat(lien,"/");
  1152.                         }
  1153.                       }
  1154.                     }
  1155.                     /* only one ending / (bug on some pages) */
  1156.                     if ((int)strlen(lien)>2) {
  1157.                       while( (lien[strlen(lien)-2]=='/') && ((int)strlen(lien)>2) )    /* double // (bug) */
  1158.                         lien[strlen(lien)-1]='\0';
  1159.                     }
  1160.                     // copier nom host si besoin est
  1161.                     if (strstr(lien,"://")==NULL) {  // pas de http://
  1162.                       char adr2[HTS_URLMAXSIZE*2],fil2[HTS_URLMAXSIZE*2];  // ** euh ident_url_relatif??
  1163.                       if (ident_url_relatif(lien,urladr,urlfil,adr2,fil2)<0) {                        
  1164.                         error=1;
  1165.                       } else {
  1166.                         strcpy(lien,"http://");
  1167.                         strcat(lien,adr2);
  1168.                         if (*fil2!='/')
  1169.                           strcat(lien,"/");
  1170.                         strcat(lien,fil2);
  1171.                         {
  1172.                           char* a;
  1173.                           a=lien+strlen(lien)-1;
  1174.                           while((*a) && (*a!='/') && ((int) a> (int) lien)) a--;
  1175.                           if (*a=='/') {
  1176.                             *(a+1)='\0';
  1177.                           }
  1178.                         }
  1179.                         //char tempo[HTS_URLMAXSIZE*2];
  1180.                         //strcpy(tempo,"http://");
  1181.                         //strcat(tempo,urladr);    // host
  1182.                         //if (*lien!='/')
  1183.                         //  strcat(tempo,"/");
  1184.                         //strcat(tempo,lien);
  1185.                         //strcpy(lien,tempo);
  1186.                       }
  1187.                     }
  1188.                     
  1189.                     if (!error) {  // pas d'erreur?
  1190.                       if (p_type==2) {   // code ET PAS codebase      
  1191.                         char* a=lien+strlen(lien)-1;
  1192.                         while( ((int) a > (int) lien) && (*a) && (*a!='/')) a--;
  1193.                         if (*a=='/')     // ok on a repΘrΘ le dernier /
  1194.                           *(a+1)='\0';   // couper
  1195.                         else {
  1196.                           *lien='\0';    // Θliminer
  1197.                           error=1;   // erreur, ne pas poursuivre
  1198.                         }      
  1199.                       }
  1200.                       
  1201.                       // stocker base ou codebase?
  1202.                       switch(p_type) {
  1203.                       case 2: { 
  1204.                         //if (*lien!='/') strcat(base,"/");
  1205.                         strcpy(base,lien);
  1206.                               }
  1207.                         break;      // base
  1208.                       case -2: {
  1209.                         //if (*lien!='/') strcat(codebase,"/");
  1210.                         strcpy(codebase,lien); 
  1211.                                }
  1212.                         break;  // base
  1213.                       }
  1214.                       
  1215.                       if ((opt.debug>1) && (opt.log!=NULL)) {
  1216.                         fspc(opt.log,"debug"); fprintf(opt.log,"code/codebase link %s base %s"LF,lien,base); test_flush;
  1217.                       }
  1218.                       //printf("base code: %s - %s\n",lien,base);
  1219.                     }
  1220.                     
  1221.                   } else {
  1222.                     char* _base;
  1223.                     if (p_type==-1)   // code (applet)
  1224.                       _base=codebase;
  1225.                     else
  1226.                       _base=base;
  1227.                     
  1228.                     // ajouter chemin de base href..
  1229.                     if (strnotempty(_base)) {       // considΘrer base
  1230.                       if (!strstr(lien,"://")) {    // non absolue
  1231.                         if (*lien!='/') {           // non absolu sur le site (/)
  1232.                           if ( ((int) strlen(_base)+(int) strlen(lien))<HTS_URLMAXSIZE) {
  1233.                             char tempo[HTS_URLMAXSIZE*2];
  1234.                             // base est absolue
  1235.                             strcpy(tempo,_base);
  1236.                             strcat(tempo,lien);
  1237.                             strcpy(lien,tempo);        // patcher en considΘrant base
  1238.                             // ** vΘrifier que ../ fonctionne (ne doit pas arriver mais bon..)
  1239.                             
  1240.                             if ((opt.debug>1) && (opt.log!=NULL)) {
  1241.                               fspc(opt.log,"debug"); fprintf(opt.log,"link modified with code/codebase %s"LF,lien); test_flush;
  1242.                             }
  1243.                           } else {
  1244.                             error=1;    // erreur
  1245.                             if (opt.errlog) {
  1246.                               fspc(opt.errlog,"error"); fprintf(opt.errlog,"Link %s too long with base href"LF,lien);
  1247.                               test_flush;
  1248.                             }
  1249.                           }
  1250.                         }
  1251.                       }
  1252.                     }
  1253.                   }
  1254.                   }
  1255.                   
  1256.                   
  1257.                   // transformer lien quelconque (http, relatif, etc) en une adresse
  1258.                   // et un chemin+fichier (adr,fil)
  1259.                   if (!error) {
  1260.                     int reponse;
  1261.                     if ((opt.debug>1) && (opt.log!=NULL)) {
  1262.                       fspc(opt.log,"debug"); fprintf(opt.log,"build relative link %s with %s%s"LF,lien,urladr,urlfil); test_flush;
  1263.                     }
  1264.                     if ((reponse=ident_url_relatif(lien,urladr,urlfil,adr,fil))<0) {                        
  1265.                       adr[0]='\0';    // erreur
  1266.                       if (reponse==-2) {
  1267.                         if (opt.errlog) {
  1268.                           fspc(opt.errlog,"warning"); fprintf(opt.errlog,"Link %s not caught (unknown ftp:// protocol)"LF,lien);
  1269.                           test_flush;
  1270.                         }
  1271.                       }
  1272.                     }
  1273.                   } else {
  1274.                     if ((opt.debug>1) && (opt.log!=NULL)) {
  1275.                       fspc(opt.log,"debug"); fprintf(opt.log,"link %s not build, error detected before"LF,lien); test_flush;
  1276.                     }
  1277.                     adr[0]='\0';
  1278.                   }
  1279.                   
  1280. #if HTS_CHECK_STRANGEDIR
  1281.                   // !ATTENTION!
  1282.                   // Ici on teste les exotiques du genre www.truc.fr/machin (sans slash α la fin)
  1283.                   // je n'ai pas encore trouvΘ le moyen de faire la diffΘrence entre un rΘpertoire
  1284.                   // et un fichier en http A PRIORI : je fais donc un test
  1285.                   // En cas de moved xxx, on recalcule adr et fil, tout simplement
  1286.                   // DEFAUT: test effectuΘ plusieurs fois! α revoir!!!
  1287.                   if ((adr[0]!='\0') && (strcmp(adr,"file://") && (p_type!=2) && (p_type!=-2)) {
  1288.                     //## if ((adr[0]!='\0') && (adr[0]!=lOCAL_CHAR) && (p_type!=2) && (p_type!=-2)) {
  1289.                     if (fil[strlen(fil)-1]!='/') {  // pas rΘpertoire
  1290.                       if (ishtml(fil)==-2) {    // pas d'extension
  1291.                         char loc[HTS_URLMAXSIZE*2];  // Θventuelle nouvelle position
  1292.                         loc[0]='\0';
  1293.                         if ((opt.debug>1) && (opt.log!=NULL)) {
  1294.                           fspc(opt.log,"debug"); fprintf(opt.log,"link-check-directory: %s%s"LF,adr,fil);
  1295.                           test_flush;
  1296.                         }
  1297.                         
  1298.                         // tester Θventuelle nouvelle position
  1299.                         switch (http_location(adr,fil,loc).statuscode) {
  1300.                         case 200: // ok au final
  1301.                           if (strnotempty(loc)) {  // a changΘ d'adresse
  1302.                             if (opt.errlog) {
  1303.                               fspc(opt.errlog,"warning"); fprintf(opt.errlog,"Link %s%s has moved to %s for %s%s"LF,adr,fil,loc,urladr,urlfil);
  1304.                               test_flush;
  1305.                             }
  1306.                             
  1307.                             // recalculer adr et fil!
  1308.                             if (ident_url(loc,adr,fil)==-1) {
  1309.                               adr[0]='\0';  // cancel
  1310.                               if ((opt.debug>1) && (opt.log!=NULL)) {
  1311.                                 fspc(opt.log,"debug"); fprintf(opt.log,"link-check-dir: %s%s"LF,adr,fil);
  1312.                                 test_flush;
  1313.                               }
  1314.                             }
  1315.                             
  1316.                           }
  1317.                           break;
  1318.                         case -2: case -3:  // timeout ou erreur grave
  1319.                           if (opt.errlog) {
  1320.                             fspc(opt.errlog,"warning"); fprintf(opt.errlog,"Connection too slow for testing link %s%s (from %s%s)"LF,adr,fil,urladr,urlfil);
  1321.                             test_flush;
  1322.                           }
  1323.                           
  1324.                           break;
  1325.                         }
  1326.                         
  1327.                       }
  1328.                     } 
  1329.                   }
  1330. #endif
  1331.                   
  1332.                   // Le lien doit juste Ωtre rΘΘcrit, mais ne doit pas gΘnΘrer un lien
  1333.                   // exemple: <FORM ACTION="url_cgi">
  1334.                   if (p_nocatch) {
  1335.                     forbidden_url=1;    // interdire rΘcupΘration du lien
  1336.                     if ((opt.debug>1) && (opt.log!=NULL)) {
  1337.                       fspc(opt.log,"debug"); fprintf(opt.log,"link ignored at %s%s"LF,adr,fil);
  1338.                       test_flush;
  1339.                     }
  1340.                   }
  1341.                   
  1342.                   // Tester si un lien doit Ωtre acceptΘ ou refusΘ (wizard)
  1343.                   // forbidden_url=1 : lien refusΘ
  1344.                   // forbidden_url=0 : lien acceptΘ
  1345.                   //if ((ptr>0) && (p_type!=2) && (p_type!=-2)) {    // tester autorisations?
  1346.                   if ((p_type!=2) && (p_type!=-2)) {    // tester autorisations?
  1347.                     if (!p_nocatch) {
  1348.                       if (adr[0]!='\0') {          
  1349.                         if ((opt.debug>1) && (opt.log!=NULL)) {
  1350.                           fspc(opt.log,"debug"); fprintf(opt.log,"wizard link test at %s%s.."LF,adr,fil);
  1351.                           test_flush;
  1352.                         }
  1353.                         forbidden_url=hts_acceptlink(&opt,ptr,lien_tot,liens,
  1354.                           adr,fil,
  1355.                           filters,&filptr,filter_max,
  1356.                           &robots,
  1357.                           &set_prio_to,
  1358.                           &just_test_it);
  1359.                         if ((opt.debug>1) && (opt.log!=NULL)) {
  1360.                           fspc(opt.log,"debug"); fprintf(opt.log,"result for wizard link test: %d"LF,forbidden_url);
  1361.                           test_flush;
  1362.                         }
  1363.                       }
  1364.                     }
  1365.                   }
  1366.                   
  1367.                   // calculer meme_adresse
  1368.                   meme_adresse=strfield2(adr,urladr);
  1369.                   
  1370.                   
  1371.                   
  1372.                   // DΘbut partie sauvegarde
  1373.                   
  1374.                   // ici on forme le nom du fichier α sauver, et on patche l'URL
  1375.                   if (adr[0]!='\0') {
  1376.                     // savename: simplifier les ../ et autres joyeusetΘs
  1377.                     char save[HTS_URLMAXSIZE*2];
  1378.                     int r_sv=0;
  1379.                     // En cas de moved, adresse premiΦre
  1380.                     char former_adr[HTS_URLMAXSIZE*2];
  1381.                     char former_fil[HTS_URLMAXSIZE*2];
  1382.                     //
  1383.                     save[0]='\0'; former_adr[0]='\0'; former_fil[0]='\0';
  1384.                     //
  1385.                     
  1386.                     // nom du chemin α sauver si on doit le calculer
  1387.                     // note: url_savename peut dΘcider de tester le lien si il le trouve
  1388.                     // suspect, et modifier alors adr et fil
  1389.                     // dans ce cas on aura une rΘfΘrence directe au lieu des traditionnels
  1390.                     // moved en cascade (impossible α reproduire α priori en local, lorsque des fichiers
  1391.                     // gif sont impliquΘs par exemple)
  1392.                     if ((p_type!=2) && (p_type!=-2)) {  // pas base href ou codebase
  1393.                       if (forbidden_url!=1) {
  1394.                         char last_adr[HTS_URLMAXSIZE*2];
  1395.                         last_adr[0]='\0';
  1396.                         //char last_fil[HTS_URLMAXSIZE*2]="";
  1397.                         strcpy(last_adr,adr);    // ancienne adresse
  1398.                         //strcpy(last_fil,fil);    // ancien chemin
  1399.                         r_sv=url_savename(adr,fil,save,former_adr,former_fil,liens[ptr]->adr,liens[ptr]->fil,&opt,liens,lien_tot,back,back_max,&cache,&hash,ptr,numero_passe);
  1400.                         if (strcmp(last_adr,adr) != 0) {  // a changΘ
  1401.                           
  1402.                           // 2e test si moved
  1403.                           
  1404.                           // Tester si un lien doit Ωtre acceptΘ ou refusΘ (wizard)
  1405.                           // forbidden_url=1 : lien refusΘ
  1406.                           // forbidden_url=0 : lien acceptΘ
  1407.                           if ((ptr>0) && (p_type!=2) && (p_type!=-2)) {    // tester autorisations?
  1408.                             if (!p_nocatch) {
  1409.                               if (adr[0]!='\0') {          
  1410.                                 if ((opt.debug>1) && (opt.log!=NULL)) {
  1411.                                   fspc(opt.log,"debug"); fprintf(opt.log,"wizard moved link retest at %s%s.."LF,adr,fil);
  1412.                                   test_flush;
  1413.                                 }
  1414.                                 forbidden_url=hts_acceptlink(&opt,ptr,lien_tot,liens,
  1415.                                   adr,fil,
  1416.                                   filters,&filptr,filter_max,
  1417.                                   &robots,
  1418.                                   &set_prio_to,
  1419.                                   &just_test_it);
  1420.                                 if ((opt.debug>1) && (opt.log!=NULL)) {
  1421.                                   fspc(opt.log,"debug"); fprintf(opt.log,"result for wizard moved link retest: %d"LF,forbidden_url);
  1422.                                   test_flush;
  1423.                                 }
  1424.                               }
  1425.                             }
  1426.                           }
  1427.                           
  1428.                           //import_done=1;    // c'est un import!
  1429.                           meme_adresse=0;   // on a changΘ
  1430.                         }
  1431.                       } else {
  1432.                         strcpy(save,"");  // dummy
  1433.                       }
  1434.                     }
  1435.                     if (r_sv!=-1) {  // pas d'erreur, on continue
  1436.                       /* log */
  1437.                       if ((opt.debug>1) && (opt.log!=NULL)) {
  1438.                         fspc(opt.log,"debug");
  1439.                         if (forbidden_url!=1) {    // le lien va Ωtre chargΘ
  1440.                           if ((p_type==2) || (p_type==-2)) {  // base href ou codebase, pas un lien
  1441.                             fprintf(opt.log,"Code/Codebase: %s%s"LF,adr,fil);
  1442.                           } else if ((opt.getmode & 4)==0) {
  1443.                             fprintf(opt.log,"Record: %s%s -> %s"LF,adr,fil,save);
  1444.                           } else {
  1445.                             if (!ishtml(fil))
  1446.                               fprintf(opt.log,"Record after: %s%s -> %s"LF,adr,fil,save);
  1447.                             else
  1448.                               fprintf(opt.log,"Record: %s%s -> %s"LF,adr,fil,save);
  1449.                           } 
  1450.                         } else
  1451.                           fprintf(opt.log,"External: %s%s"LF,adr,fil);
  1452.                         test_flush;
  1453.                       }
  1454.                       /* FIN log */
  1455.                       
  1456.                       // Θcrire lien
  1457.                       if ((p_type==2) || (p_type==-2)) {  // base href ou codebase, sauter
  1458.                         lastsaved=eadr-1+1;  // sauter "
  1459.                       } else if (forbidden_url==1) {    // le lien ne sera pas chargΘ, rΘfΘrence externe!
  1460.                         if ((opt.getmode & 1) && (ptr>0)) {
  1461.                           if (p_type!=-1) {     // pas que le nom de fichier (pas classe java)
  1462.                             if (!opt.external) {
  1463.                               if (!strstr(adr,"://")) {
  1464.                                 HT_ADD("http://");
  1465.                               }
  1466.                               HT_ADD(adr);
  1467.                               if (*fil!='/')
  1468.                                 HT_ADD("/");
  1469.                               HT_ADD(fil);
  1470.                               //
  1471.                             } else {    // fichier/page externe, mais on veut gΘnΘrer une erreur
  1472.                               //
  1473.                               int patch_it=0;
  1474.                               int add_url=0;
  1475.                               char* cat_name=NULL;
  1476.                               char* cat_data=NULL;
  1477.                               int cat_data_len=0;
  1478.                               
  1479.                               // ajouter lien external
  1480.                               switch ((fil[strlen(fil)-1]=='/')?1:(ishtml(fil))) {
  1481.                               case 1: case -2:       // html ou rΘpertoire
  1482.                                 if (opt.getmode & 1) {  // sauver html
  1483.                                   patch_it=1;   // redirect
  1484.                                   add_url=1;    // avec link?
  1485.                                   cat_name="external.html";
  1486.                                   cat_data=HTS_DATA_UNKNOWN_HTML;
  1487.                                   cat_data_len=HTS_DATA_UNKNOWN_HTML_LEN;
  1488.                                 }
  1489.                                 break;
  1490.                               default:    // inconnu
  1491.                                 if ( (strfield2(fil+strlen(fil)-4,".gif")) 
  1492.                                   || (strfield2(fil+strlen(fil)-4,".jpg")) 
  1493.                                   || (strfield2(fil+strlen(fil)-4,".xbm")) 
  1494.                                   || (ishtml(fil)!=0) ) {
  1495.                                   patch_it=1;   // redirect
  1496.                                   add_url=1;    // avec link aussi
  1497.                                   cat_name="external.gif";
  1498.                                   cat_data=HTS_DATA_UNKNOWN_GIF;
  1499.                                   cat_data_len=HTS_DATA_UNKNOWN_GIF_LEN;
  1500.                                 }
  1501.                                 break;
  1502.                               }// html,gif
  1503.                               
  1504.                               if (patch_it) {
  1505.                                 char save[HTS_URLMAXSIZE*2];
  1506.                                 char tempo[HTS_URLMAXSIZE*2];
  1507.                                 strcpy(save,opt.path_html);
  1508.                                 strcat(save,cat_name);
  1509.                                 if (lienrelatif(tempo,save,savename)==0) {
  1510.                                   HT_ADD(tempo);    // page externe
  1511.                                   if (add_url) {
  1512.                                     HT_ADD("?link=");    // page externe
  1513.                                     HT_ADD(adr);
  1514.                                     if (*fil!='/')
  1515.                                       HT_ADD("/");
  1516.                                     HT_ADD(fil);
  1517.                                   }
  1518.                                 }
  1519.                                 
  1520.                                 // Θcrire fichier?
  1521.                                 if (!fexist(fconcat(opt.path_html,cat_name))) {
  1522.                                   FILE* fp = filecreate(fconcat(opt.path_html,cat_name));
  1523.                                   if (fp) {
  1524.                                     if (cat_data_len==0) {   // texte
  1525.                                       verif_backblue(opt.path_html);
  1526.                                       fprintf(fp,"%s%s","<!-- Created by HTTrack Website Copier/"HTTRACK_VERSION" "HTTRACK_AFF_AUTHORS" -->"LF,cat_data);
  1527.                                     } else {                    // data
  1528.                                       fwrite(cat_data,cat_data_len,1,fp);
  1529.                                     }
  1530.                                     fclose(fp);
  1531.                                     usercommand(0,NULL,fconcat(opt.path_html,cat_name));
  1532.                                   }
  1533.                                 }
  1534.                               }  else {    // Θcrire normalement le nom de fichier
  1535.                                 HT_ADD("http://");
  1536.                                 HT_ADD(adr);
  1537.                                 if (*fil!='/')
  1538.                                   HT_ADD("/");
  1539.                                 HT_ADD(fil);
  1540.                               }// patcher?
  1541.                             }  // external
  1542.                           } else {  // que le nom de fichier (classe java)
  1543.                             // en gros recopie de plus bas: copier codebase et base
  1544.                             if (p_flush) {
  1545.                               char tempo[HTS_URLMAXSIZE*2];    // <-- ajoutΘ
  1546.                               char tempo_pat[HTS_URLMAXSIZE*2];
  1547.                               tempo_pat[0]='\0';
  1548.                               strcpy(tempo,fil);  // <-- ajoutΘ
  1549.                               {
  1550.                                 char* a=tempo+strlen(tempo)-1;
  1551.                                 while( ((int) a > (int) tempo) && (*a) && (*a!='/')) a--;
  1552.                                 if (*a=='/') {
  1553.                                   char tempo2[HTS_URLMAXSIZE*2];
  1554.                                   strcpy(tempo2,a+1);
  1555.                                   strncat(tempo_pat,tempo,(int) a-(int) tempo+1);  // chemin
  1556.                                   strcpy(tempo,tempo2);                     // fichier
  1557.                                 }
  1558.                               }
  1559.                               
  1560.                               // Θrire codebase="chemin"
  1561.                               if ((opt.getmode & 1) && (ptr>0)) {
  1562.                                 char tempo4[HTS_URLMAXSIZE*2];
  1563.                                 tempo4[0]='\0';
  1564.                                 
  1565.                                 if (strnotempty(tempo_pat)) {
  1566.                                   HT_ADD("codebase=\"http://");
  1567.                                   HT_ADD(adr);
  1568.                                   if (*tempo_pat!='/') HT_ADD("/");
  1569.                                   HT_ADD(tempo_pat);
  1570.                                   HT_ADD("\" ");
  1571.                                 }
  1572.                                 
  1573.                                 strncat(tempo4,lastsaved,(int) p_flush-(int) lastsaved);
  1574.                                 HT_ADD(tempo4);    // refresh code="
  1575.                                 HT_ADD(tempo);
  1576.                               }
  1577.                             }
  1578.                           }
  1579.                         }
  1580.                         lastsaved=eadr-1;
  1581.                       } 
  1582.                       /*
  1583.                       else if (opt.urlmode==1) {    // ABSOLU, c'est le cas le moins courant
  1584.                       //  NE FONCTIONNE PAS!!  (et est inutile)
  1585.                       if ((opt.getmode & 1) && (ptr>0)) {    // ecrire les html
  1586.                       // Θcrire le lien modifiΘ, absolu
  1587.                       HT_ADD("file:");
  1588.                       if (*save=='/')
  1589.                       HT_ADD(save+1)
  1590.                       else
  1591.                       HT_ADD(save)
  1592.                       }
  1593.                       lastsaved=eadr-1;    // dernier Θcrit+1 (enfin euh apres on fait un ++ alors hein)
  1594.                       }
  1595.                       */
  1596.                       else if (opt.urlmode==3) {    // URI absolue /
  1597.                         if ((opt.getmode & 1) && (ptr>0)) {    // ecrire les html
  1598.                           HT_ADD(fil);
  1599.                         }
  1600.                         lastsaved=eadr-1;    // dernier Θcrit+1 (enfin euh apres on fait un ++ alors hein)
  1601.                       }
  1602.                       else if (opt.urlmode==2) {  // RELATIF
  1603.                         char tempo[HTS_URLMAXSIZE*2];
  1604.                         tempo[0]='\0';
  1605.                         // calculer le lien relatif
  1606.                         
  1607.                         if (lienrelatif(tempo,save,savename)==0) {
  1608.                           if ((opt.debug>1) && (opt.log!=NULL)) {
  1609.                             fspc(opt.log,"debug"); fprintf(opt.log,"relative link at %s build with %s and %s: %s"LF,adr,save,savename,tempo);
  1610.                             test_flush;
  1611.                           }
  1612.                           
  1613.                           // lien applet (code) - il faut placer un codebase avant
  1614.                           if (p_type==-1) {  // que le nom de fichier
  1615.                             
  1616.                             if (p_flush) {
  1617.                               char tempo_pat[HTS_URLMAXSIZE*2];
  1618.                               tempo_pat[0]='\0';
  1619.                               {
  1620.                                 char* a=tempo+strlen(tempo)-1;
  1621.                                 while( ((int) a > (int) tempo) && (*a) && (*a!='/')) a--;
  1622.                                 if (*a=='/') {
  1623.                                   char tempo2[HTS_URLMAXSIZE*2];
  1624.                                   strcpy(tempo2,a+1);
  1625.                                   strncat(tempo_pat,tempo,(int) a-(int) tempo+1);  // chemin
  1626.                                   strcpy(tempo,tempo2);                     // fichier
  1627.                                 }
  1628.                               }
  1629.                               
  1630.                               // Θrire codebase="chemin"
  1631.                               if ((opt.getmode & 1) && (ptr>0)) {
  1632.                                 char tempo4[HTS_URLMAXSIZE*2];
  1633.                                 tempo4[0]='\0';
  1634.                                 
  1635.                                 if (strnotempty(tempo_pat)) {
  1636.                                   HT_ADD("codebase=\"");
  1637.                                   HT_ADD(tempo_pat);
  1638.                                   HT_ADD("\" ");
  1639.                                 }
  1640.                                 
  1641.                                 strncat(tempo4,lastsaved,(int) p_flush-(int) lastsaved);
  1642.                                 HT_ADD(tempo4);    // refresh code="
  1643.                               }
  1644.                             }
  1645.                             //lastsaved=adr;    // dernier Θcrit+1
  1646.                           }                              
  1647.                           
  1648.                           if ((opt.getmode & 1) && (ptr>0)) {
  1649.                             // Θcrire le lien modifiΘ, relatif
  1650.                             HT_ADD(tempo);
  1651.                           }
  1652.                           lastsaved=eadr-1;    // dernier Θcrit+1 (enfin euh apres on fait un ++ alors hein)
  1653.                         } else {
  1654.                           if (opt.errlog) {
  1655.                             fprintf(opt.errlog,"Error building relative link %s and %s"LF,save,savename);
  1656.                             test_flush;
  1657.                           }
  1658.                         }
  1659.                       }  // sinon le lien sera Θcrit normalement
  1660.                       
  1661.                       
  1662. #if 0
  1663.                       if (fexist(save)) {    // le fichier existe..
  1664.                         adr[0]='\0';
  1665.                         //if ((opt.debug>0) && (opt.log!=NULL)) {
  1666.                         if (opt.errlog) {
  1667.                           fspc(opt.errlog,"warning"); fprintf(opt.errlog,"Link has already been written on disk, cancelled: %s"LF,save);
  1668.                           test_flush;
  1669.                         }
  1670.                       }
  1671. #endif                            
  1672.                       
  1673.                       if ((adr[0]!='\0') && (p_type!=2) && (p_type!=-2) && ( (forbidden_url!=1) || (just_test_it))) {  // si le fichier n'existe pas, ajouter α la liste                            
  1674.                         // n'y a-t-il pas trop de liens?
  1675.                         if (lien_tot+1 >= lien_max-4) {    // trop de liens!
  1676.                           printf("PANIC! : Too many URLs : >%d [%d]\n",lien_tot,__LINE__);
  1677.                           if (opt.errlog) {
  1678.                             fprintf(opt.errlog,LF"Too many URLs, giving up..(>%d)"LF,lien_max);
  1679.                             fprintf(opt.errlog,"To avoid that: use #L option for more links (example: -#L1000000)"LF);
  1680.                             test_flush;
  1681.                           }
  1682.                           if ((opt.getmode & 1) && (ptr>0)) { if (fp) { fclose(fp); fp=NULL; } }
  1683.                           XH_uninit;   // dΘsallocation mΘmoire & buffers
  1684.                           return 0;
  1685.                           
  1686.                         } else {    // noter le lien sur la listes des liens α charger
  1687.                           int pass_fix,dejafait=0;
  1688.                           
  1689.                           // Calculer la prioritΘ de ce lien
  1690.                           if ((opt.getmode & 4)==0) {    // traiter html aprΦs
  1691.                             pass_fix=0;
  1692.                           } else {    // vΘrifier que ce n'est pas un !html
  1693.                             if (!ishtml(fil))
  1694.                               pass_fix=1;        // prioritΘ infΘrieure (traiter aprΦs)
  1695.                             else
  1696.                               pass_fix=max(0,numero_passe);    // prioritΘ normale
  1697.                           }
  1698.                           
  1699.                           // vΘrifier que le lien n'a pas dΘja ΘtΘ notΘ
  1700.                           // si c'est le cas, alors il faut s'assurer que la prioritΘ associΘe
  1701.                           // au fichier est la plus grande des deux prioritΘs
  1702.                           //
  1703.                           // On part de la fin et on essaye de se presser (Θconomise temps machine)
  1704. #if HTS_HASH
  1705.                           {
  1706.                             int i=hash_read(&hash,save,"",0);      // lecture type 0 (sav)
  1707.                             if (i>=0) {
  1708.                               liens[i]->depth=maximum(liens[i]->depth,liens[ptr]->depth-1);
  1709.                               dejafait=1;
  1710.                             }
  1711.                           }
  1712. #else
  1713.                           {
  1714.                             register int l;
  1715.                             register int i;
  1716.                             l=strlen(save);  // opti
  1717.                             for(i=lien_tot-1;(i>=0) && (dejafait==0);i--) {
  1718.                               if (liens[i]->sav_len==l) {    // mΩme taille de chaεne
  1719.                                 if (strcmp(liens[i]->sav,save)==0) {    // existe dΘja
  1720.                                   liens[i]->depth=maximum(liens[i]->depth,liens[ptr]->depth-1);
  1721.                                   dejafait=1;
  1722.                                 }
  1723.                               }
  1724.                             }
  1725.                           }
  1726. #endif
  1727.                           
  1728.                           // le lien n'a jamais ΘtΘ crΘΘ.
  1729.                           // cette fois ci, on le crΘe!
  1730.                           if (!dejafait) {                                
  1731.                             //
  1732.                             // >>>> CREER LE LIEN <<<<
  1733.                             //
  1734.                             // enregistrer lien α charger
  1735.                             //liens[lien_tot]->adr[0]=liens[lien_tot]->fil[0]=liens[lien_tot]->sav[0]='\0';
  1736.                             // mΩme adresse: l'objet pΦre est l'objet pΦre de l'actuel
  1737.                             
  1738.                             // DEBUT ROBOTS.TXT AJOUT
  1739.                             if (!just_test_it) {
  1740.                               if (strfield(adr,"ftp://")==0) {    // non ftp
  1741.                                 if (opt.robots) {    // rΘcupΘrer robots
  1742.                                   if (ishtml(fil)!=0) {                       // pas la peine pour des fichiers isolΘs
  1743.                                     if (checkrobots(&robots,adr,"") != -1) {    // robots.txt ?
  1744.                                       checkrobots_set(&robots,adr,"");          // ajouter entrΘe vide
  1745.                                       if (checkrobots(&robots,adr,"") == -1) {    // robots.txt ?
  1746.                                         // enregistrer robots.txt (MACRO)
  1747.                                         liens_record(adr,"/robots.txt","","","");
  1748.                                         if (liens[lien_tot]==NULL) {  // erreur, pas de place rΘservΘe
  1749.                                           printf("PANIC! : Not enough memory [%d]\n",__LINE__);
  1750.                                           if (opt.errlog) { 
  1751.                                             fprintf(opt.errlog,"Not enough memory, can not re-allocate %d bytes"LF,(add_tab_alloc+1)*sizeof(lien_url));
  1752.                                             test_flush;
  1753.                                           }
  1754.                                           if ((opt.getmode & 1) && (ptr>0)) { if (fp) { fclose(fp); fp=NULL; } }
  1755.                                           XH_uninit;    // dΘsallocation mΘmoire & buffers
  1756.                                           return 0;
  1757.                                         }  
  1758.                                         liens[lien_tot]->testmode=0;          // pas mode test
  1759.                                         liens[lien_tot]->link_import=0;       // pas mode import     
  1760.                                         liens[lien_tot]->premier=lien_tot;
  1761.                                         liens[lien_tot]->precedent=ptr;
  1762.                                         liens[lien_tot]->depth=0;
  1763.                                         liens[lien_tot]->pass2=max(0,numero_passe);
  1764.                                         liens[lien_tot]->retry=0;
  1765.                                         lien_tot++;  // UN LIEN DE PLUS
  1766. #if DEBUG_ROBOTS
  1767.                                         printf("robots.txt: added file robots.txt for %s\n",adr);
  1768. #endif
  1769.                                         if ((opt.debug>1) && (opt.log!=NULL)) {
  1770.                                           fspc(opt.log,"debug"); fprintf(opt.log,"robots.txt added at %s"LF,adr);
  1771.                                           test_flush;
  1772.                                         }
  1773.                                       } else {
  1774.                                         if (opt.errlog) {   
  1775.                                           fprintf(opt.errlog,"Unexpected robots.txt error at %d"LF,__LINE__);
  1776.                                           test_flush;
  1777.                                         }
  1778.                                       }
  1779.                                     }
  1780.                                   }
  1781.                                 }
  1782.                               }
  1783.                             }
  1784.                             // FIN ROBOTS.TXT AJOUT
  1785.                             
  1786.                             // enregistrer (MACRO)
  1787.                             liens_record(adr,fil,save,former_adr,former_fil);
  1788.                             if (liens[lien_tot]==NULL) {  // erreur, pas de place rΘservΘe
  1789.                               printf("PANIC! : Not enough memory [%d]\n",__LINE__);
  1790.                               if (opt.errlog) { 
  1791.                                 fprintf(opt.errlog,"Not enough memory, can not re-allocate %d bytes"LF,(add_tab_alloc+1)*sizeof(lien_url));
  1792.                                 test_flush;
  1793.                               }
  1794.                               if ((opt.getmode & 1) && (ptr>0)) { if (fp) { fclose(fp); fp=NULL; } }
  1795.                               XH_uninit;    // dΘsallocation mΘmoire & buffers
  1796.                               return 0;
  1797.                             }  
  1798.                             
  1799.                             // mode test?
  1800.                             if (!just_test_it)
  1801.                               liens[lien_tot]->testmode=0;          // pas mode test
  1802.                             else
  1803.                               liens[lien_tot]->testmode=1;          // mode test
  1804.                             if (!import_done)
  1805.                               liens[lien_tot]->link_import=0;       // pas mode import
  1806.                             else
  1807.                               liens[lien_tot]->link_import=1;       // mode import
  1808.                             // Θcrire autres paramΦtres de la structure-lien
  1809.                             if ((meme_adresse) && (!import_done) && (liens[ptr]->premier != 0))
  1810.                               liens[lien_tot]->premier=liens[ptr]->premier;
  1811.                             else    // sinon l'objet pΦre est le prΘcΘdent lui mΩme
  1812.                               liens[lien_tot]->premier=lien_tot;
  1813.                             // liens[lien_tot]->premier=ptr;
  1814.                             
  1815.                             liens[lien_tot]->precedent=ptr;
  1816.                             // noter la prioritΘ
  1817.                             if (!set_prio_to)
  1818.                               liens[lien_tot]->depth=liens[ptr]->depth-1;
  1819.                             else
  1820.                               liens[lien_tot]->depth=max(0,min(liens[ptr]->depth-1,set_prio_to-1));         // PRIORITE NULLE (catch page)
  1821.                             // noter pass
  1822.                             liens[lien_tot]->pass2=pass_fix;
  1823.                             liens[lien_tot]->retry=opt.retry;
  1824.                             
  1825.                             //strcpy(liens[lien_tot]->adr,adr);
  1826.                             //strcpy(liens[lien_tot]->fil,fil);
  1827.                             //strcpy(liens[lien_tot]->sav,save); 
  1828.                             if ((opt.debug>1) && (opt.log!=NULL)) {
  1829.                               if (!just_test_it) {
  1830.                                 fspc(opt.log,"debug"); fprintf(opt.log,"OK, NOTE: %s%s -> %s"LF,liens[lien_tot]->adr,liens[lien_tot]->fil,liens[lien_tot]->sav);
  1831.                               } else {
  1832.                                 fspc(opt.log,"debug"); fprintf(opt.log,"OK, TEST: %s%s"LF,liens[lien_tot]->adr,liens[lien_tot]->fil);
  1833.                               }
  1834.                               test_flush;
  1835.                             }
  1836.                             
  1837.                             lien_tot++;  // UN LIEN DE PLUS
  1838.                           } else { // if !dejafait
  1839.                             if ((opt.debug>1) && (opt.log!=NULL)) {
  1840.                               fspc(opt.log,"debug"); fprintf(opt.log,"link has already been recorded, cancelled: %s"LF,save);
  1841.                               test_flush;
  1842.                             }
  1843.                             
  1844.                           }
  1845.                           
  1846.                           
  1847.                         }   // si pas trop de liens
  1848.                       }   // si adr[0]!='\0'
  1849.                       
  1850.                       
  1851.                     }  // if adr[0]!='\0' 
  1852.                     
  1853.                   }  // if adr[0]!='\0'
  1854.                   
  1855.                 }    // if strlen(lien)>0
  1856.                 
  1857.               }   // if ok==0      
  1858.               
  1859.               adr=eadr-1;  // ** sauter
  1860.               
  1861.             }  // if (p) 
  1862.             
  1863.           }  // si '<' ou '>'
  1864.           
  1865.           // plus loin
  1866.           adr++;  
  1867.           
  1868.           // ----------
  1869.           // Θcrire peu α peu
  1870.           if ((opt.getmode & 1) && (ptr>0)) HT_ADD_ADR;
  1871.           lastsaved=adr;    // dernier Θcrit+1
  1872.           // ----------
  1873.           
  1874.           // pour les stats du shell si parsing trop long
  1875. #if HTS_ANALYSTE==2
  1876.           _hts_in_html_done=(100 * ((int) adr - (int) r.adr) ) / (int)(r.size);
  1877.           if (_hts_in_html_poll) {
  1878.             LLint nb;
  1879.             int nbk;
  1880.             _hts_in_html_poll=0;
  1881.             // temps α attendre, et remplir autant que l'on peut le cache (backing)
  1882.             back_wait(back,back_max,&opt,&cache,HTS_STAT.stat_timestart);        
  1883.             back_fillmax(back,back_max,&opt,&cache,liens,ptr,numero_passe,lien_tot);
  1884.  
  1885.             engine_stats();
  1886.             nb=back_transfered(HTS_STAT.stat_bytes,back,back_max);
  1887.             nbk=backlinks_done(liens,lien_tot,ptr);
  1888.             //if (!hts_htmlcheck_loop(back,back_max,-1,ptr,lien_tot,nb,new_stat_bytes,(int) (time_local()-stat_timestart),back_nsoc(back,back_max) )) {
  1889.             if (!hts_htmlcheck_loop(back,back_max,0,ptr,lien_tot,nb,new_stat_bytes,(int) (time_local()-HTS_STAT.stat_timestart),back_nsoc(back,back_max), HTS_STAT.stat_files,HTS_STAT.stat_updated_files,fspc(NULL,"error"),(int)HTS_STAT.rate,nbk )) {
  1890.               if (opt.errlog) {
  1891.                 fspc(opt.errlog,"info"); fprintf(opt.errlog,"Exit requested by shell or user"LF);
  1892.                 test_flush;
  1893.               } 
  1894.               exit_xh=1;  // exit requested
  1895.               XH_uninit;
  1896.               return 0;
  1897.               //adr = r.adr + r.size;  // exit
  1898.             } else if (_hts_cancel==1) {
  1899.               adr = r.adr + r.size;  // exit
  1900.               _hts_cancel=0;
  1901.             }
  1902.           }
  1903.  
  1904.           // refresh the backing system each 2 seconds
  1905.           if (engine_stats()) {
  1906.             back_wait(back,back_max,&opt,&cache,HTS_STAT.stat_timestart);        
  1907.             back_fillmax(back,back_max,&opt,&cache,liens,ptr,numero_passe,lien_tot);
  1908.           }
  1909. #endif
  1910.         } while(( ((int) adr) - ((int) r.adr) ) < r.size);
  1911. #if HTS_ANALYSTE==2
  1912.         _hts_in_html_parsing=0;  // flag
  1913.         _hts_cancel=0;           // pas de cancel
  1914. #endif
  1915.         if ((opt.getmode & 1) && (ptr>0)) {
  1916.           HT_ADD_END;    // achever
  1917.         }
  1918.         //
  1919.         //
  1920.         //
  1921.       }  // if !error
  1922.       
  1923.       
  1924.       if (opt.getmode & 1) { if (fp) { fclose(fp); fp=NULL; } }
  1925.       // sauver fichier
  1926.       //structcheck(savename);
  1927.       //filesave(r.adr,r.size,savename);
  1928.       
  1929. #if HTS_ANALYSTE
  1930.     }  // analyse OK
  1931. #endif
  1932.         
  1933.